<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GitHub仓库克隆 - 文件共享平台</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}" type="image/x-icon">
    <style>
        .github-card {
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            margin-bottom: 20px;
            overflow: hidden;
        }
        
        .github-header {
            background: linear-gradient(135deg, #24292e, #2b3137);
            color: white;
            padding: 15px 20px;
        }
        
        .github-body {
            padding: 20px;
            background-color: #f6f8fa;
        }
        
        .task-card {
            background-color: white;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 10px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.05);
        }
        
        .task-status {
            display: inline-block;
            padding: 3px 8px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: 500;
        }
        
        .status-pending {
            background-color: #6c757d;
            color: white;
        }
        
        .status-cloning {
            background-color: #0d6efd;
            color: white;
        }
        
        .status-completed {
            background-color: #198754;
            color: white;
        }
        
        .status-failed {
            background-color: #dc3545;
            color: white;
        }
        
        .status-cancelled {
            background-color: #ffc107;
            color: black;
        }
        
        .progress {
            height: 8px;
            margin: 10px 0;
        }
        
        .speed-badge {
            font-size: 12px;
            padding: 2px 6px;
            border-radius: 10px;
            background-color: #e9ecef;
        }
        
        .task-actions {
            display: flex;
            gap: 10px;
            margin-top: 10px;
        }

        /* 深色模式变量 */
        :root {
            --primary: #4361ee;
            --text-color: #333;
            --bg-color: #f5f7fb;
            --card-bg: #ffffff;
            --header-bg: linear-gradient(135deg, #24292e, #2b3137);
            --border-color: #dee2e6;
            --input-bg: #ffffff;
            --text-muted: #6c757d;
            --github-body-bg: #f6f8fa;
            --task-card-bg: #ffffff;
            --text-color-no-dark: #212529; /* 不随深色模式改变的文本颜色 */
        }
        
        [data-theme="dark"] {
            --primary: #5a76ff;
            --text-color: #e0e0e0;
            --bg-color: #121418;
            --card-bg: #1e2229;
            --header-bg: linear-gradient(135deg, #2a3a8d, #1a1a5e);
            --border-color: #2d333b;
            --input-bg: #252b33;
            --text-muted: #a0a8b0;
            --github-body-bg: #1a1c23;
            --task-card-bg: #252b33;
        }
        
        /* 不随深色模式改变颜色的文本 */
        .text-no-dark {
            color: var(--text-color-no-dark);
        }
        
        /* 深色模式下的模态框样式 */
        [data-theme="dark"] .modal-content {
            background-color: var(--input-bg);
            color: var(--text-color);
            border-color: var(--border-color);
        }
        
        [data-theme="dark"] .modal-header {
            border-bottom-color: var(--border-color);
        }
        
        [data-theme="dark"] .modal-footer {
            border-top-color: var(--border-color);
        }
        
        [data-theme="dark"] .btn-secondary {
            background-color: var(--input-bg);
            border-color: var(--border-color);
            color: var(--text-color);
        }
        
        /* 深色模式下的消息提示样式 */
        [data-theme="dark"] #message-box .toast {
            background-color: var(--input-bg);
            color: var(--text-color);
            border: 1px solid var(--border-color);
        }
        
        [data-theme="dark"] #message-box .toast-header {
            background-color: var(--card-bg);
            border-bottom-color: var(--border-color);
        }
        
        body {
            background-color: var(--bg-color);
            color: var(--text-color);
            transition: background-color 0.3s, color 0.3s;
        }
        
        .github-card {
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            margin-bottom: 20px;
            overflow: hidden;
            background-color: var(--card-bg);
            border: 1px solid var(--border-color);
            transition: background-color 0.3s, border-color 0.3s;
        }
        
        .github-header {
            background: var(--header-bg);
            color: white;
            padding: 15px 20px;
        }
        
        .github-body {
            padding: 20px;
            background-color: var(--github-body-bg);
            transition: background-color 0.3s;
        }
        
        .task-card {
            background-color: var(--task-card-bg);
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 10px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.05);
            color: var(--text-color);
            transition: background-color 0.3s, color 0.3s;
        }
        
        .task-status {
            display: inline-block;
            padding: 3px 8px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: 500;
        }
        
        .status-pending {
            background-color: #6c757d;
            color: white;
        }
        
        .status-cloning {
            background-color: #0d6efd;
            color: white;
        }
        
        .status-completed {
            background-color: #198754;
            color: white;
        }
        
        .status-failed {
            background-color: #dc3545;
            color: white;
        }
        
        .status-cancelled {
            background-color: #ffc107;
            color: black;
        }
        
        .progress {
            height: 8px;
            margin: 10px 0;
            background-color: var(--input-bg);
        }
        
        .progress-bar {
            background-color: var(--primary);
        }
        
        .speed-badge {
            font-size: 12px;
            padding: 2px 6px;
            border-radius: 10px;
            background-color: var(--input-bg);
            color: var(--text-color);
            transition: background-color 0.3s, color 0.3s;
        }
        
        .task-actions {
            display: flex;
            gap: 10px;
            margin-top: 10px;
        }

        .theme-switch-container {
            position: fixed;
            top: 15px;
            right: 15px;
            z-index: 1000;
        }
        
        #theme-toggle {
            background-color: var(--card-bg);
            color: var(--text-color);
            border: 1px solid var(--border-color);
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            transition: background-color 0.3s, color 0.3s, border-color 0.3s;
        }
        
        .form-control {
            background-color: var(--input-bg);
            color: var(--text-color);
            border: 1px solid var(--border-color);
            transition: background-color 0.3s, color 0.3s, border-color 0.3s;
        }
        
        .form-control:focus {
            background-color: var(--input-bg);
            color: var(--text-color);
            border-color: var(--primary);
        }
        
        .alert {
            background-color: var(--input-bg);
            color: var(--text-color);
            border: 1px solid var(--border-color);
            transition: background-color 0.3s, color 0.3s, border-color 0.3s;
        }
    </style>
</head>
<body>
    <div class="theme-switch-container">
        <button id="theme-toggle" class="btn btn-sm btn-outline-secondary">
            <i class="bi bi-moon"></i> 深色模式
        </button>
    </div>

    <div class="container py-4">
        <div class="github-card">

            <div class="github-header">
                <a href="/admin" class="btn btn-dark btn-light" title="返回主页">
                    <h2><i class="bi bi-github"></i> GitHub仓库克隆(点我返回管理页)</h2>
                </a>
                <p class="mb-0">克隆GitHub仓库并保存到上传目录</p>
            </div>
            
            <div class="github-body">
                <div class="mb-3">
                    <label class="form-label fw-bold">GitHub仓库URL</label>
                    <input type="text" class="form-control" id="github-repo-url" 
                           placeholder="https://github.com/user/repo 或 https://github.com/user/repo.git">
                </div>
                
                <div class="row mb-4">
                    <div class="col-md-6">
                        <label class="form-label fw-bold">分支 (默认: main)</label>
                        <input type="text" class="form-control" id="github-branch" value="main">
                    </div>
                </div>
                
                <div class="d-flex justify-content-end">
                    <button id="start-clone-btn" class="btn btn-success">
                        <i class="bi bi-download me-1"></i> 开始克隆
                    </button>
                </div>
            </div>
        </div>
        
        <!-- 克隆任务列表 -->
        <div class="github-card">
            <div class="github-header d-flex justify-content-between align-items-center">
                <h3><i class="bi bi-list-task me-2"></i> 克隆任务列表</h3>
                <div>
                    <button id="clear-completed-btn" class="btn btn-sm btn-outline-success me-2">
                        <i class="bi bi-check-circle"></i> 清除已完成
                    </button>
                    <button id="clear-failed-btn" class="btn btn-sm btn-outline-danger">
                        <i class="bi bi-x-circle"></i> 清除失败
                    </button>
                </div>
            </div>
            
            <div class="github-body">
                <div id="tasks-container">
                    <div class="text-center py-4">
                        <div class="spinner-border text-primary" role="status">
                            <span class="visually-hidden">加载中...</span>
                        </div>
                        <p class="mt-2">正在加载任务列表...</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const startCloneBtn = document.getElementById('start-clone-btn');
            const clearCompletedBtn = document.getElementById('clear-completed-btn');
            const clearFailedBtn = document.getElementById('clear-failed-btn');
            const tasksContainer = document.getElementById('tasks-container');
            
            // 加载任务列表
            function loadTasks() {
                fetch('/api/github_clone/tasks')
                    .then(response => {
                        if (!response.ok) {
                            throw new Error('网络响应异常');
                        }
                        return response.json();
                    })
                    .then(data => {
                        if (data.tasks && Object.keys(data.tasks).length > 0) {
                            renderTasks(data.tasks);
                        } else {
                            tasksContainer.innerHTML = `
                                <div class="text-center py-4">
                                    <i class="bi bi-inbox display-4 text-muted mb-3"></i>
                                    <h5 class="text-muted">暂无克隆任务</h5>
                                    <p>添加GitHub仓库URL开始克隆</p>
                                </div>
                            `;
                        }
                    })
                    .catch(error => {
                        console.error('加载任务失败:', error);
                        tasksContainer.innerHTML = `
                            <div class="alert alert-danger">
                                <i class="bi bi-exclamation-triangle"></i> 加载任务列表失败: ${error.message}
                            </div>
                        `;
                    });
            }
            
            // 渲染任务列表
            function renderTasks(tasks) {
                let html = '';
                
                for (const [taskId, task] of Object.entries(tasks)) {
                    const status = task.status || 'pending';
                    const repoName = task.repo_url ? task.repo_url.split('/').pop().replace('.git', '') : '未知仓库';
                    const fileName = task.file_name || '准备中...';
                    const progress = task.progress || 0;
                    
                    html += `
                        <div class="task-card">
                            <div class="d-flex justify-content-between align-items-center mb-2">
                                <div>
                                    <h5 class="mb-1">${repoName}</h5>
                                    <p class="text-muted small mb-0">${task.repo_url}</p>
                                </div>
                                <span class="task-status status-${status}">
                                    ${status === 'cloning' ? '克隆中' : 
                                      status === 'completed' ? '已完成' : 
                                      status === 'failed' ? '失败' : 
                                      status === 'cancelled' ? '已取消' : '等待中'}
                                </span>
                            </div>
                            
                            <div class="d-flex justify-content-between small text-muted mb-2">
                                <span>分支: ${task.branch || 'main'}</span>
                                <span>文件: ${fileName}</span>
                            </div>
                            
                            ${status === 'cloning' ? `
                                <div class="progress">
                                    <div class="progress-bar" role="progressbar" style="width: ${progress}%"></div>
                                </div>
                                <div class="d-flex justify-content-between small text-muted">
                                    <span>${progress.toFixed(1)}%</span>
                                    <span class="speed-badge"><i class="bi bi-clock"></i> ${formatElapsed(time.time() - task.start_time)}</span>
                                </div>
                            ` : ''}
                            
                            ${status === 'completed' ? `
                                <div class="alert alert-success mb-0 mt-2">
                                    <i class="bi bi-check-circle"></i> 克隆完成! 文件已保存到上传目录
                                </div>
                            ` : ''}
                            
                            ${status === 'failed' ? `
                                <div class="alert alert-danger mb-0 mt-2">
                                    <i class="bi bi-exclamation-circle"></i> 克隆失败: ${task.error || '未知错误'}
                                </div>
                            ` : ''}
                            
                            ${status === 'cancelled' ? `
                                <div class="alert alert-warning mb-0 mt-2">
                                    <i class="bi bi-slash-circle"></i> 任务已取消
                                </div>
                            ` : ''}
                            
                            <div class="task-actions">
                                ${status === 'cloning' || status === 'pending' ? `
                                    <button class="btn btn-sm btn-outline-danger cancel-btn" data-task-id="${taskId}">
                                        <i class="bi bi-x-circle"></i> 取消
                                    </button>
                                ` : ''}
                                
                                <button class="btn btn-sm btn-outline-secondary remove-btn" data-task-id="${taskId}">
                                    <i class="bi bi-trash"></i> 移除
                                </button>
                            </div>
                        </div>
                    `;
                }
                
                tasksContainer.innerHTML = html;
                
                // 添加取消按钮事件
                document.querySelectorAll('.cancel-btn').forEach(btn => {
                    btn.addEventListener('click', function() {
                        const taskId = this.getAttribute('data-task-id');
                        cancelTask(taskId);
                    });
                });
                
                // 添加移除按钮事件
                document.querySelectorAll('.remove-btn').forEach(btn => {
                    btn.addEventListener('click', function() {
                        const taskId = this.getAttribute('data-task-id');
                        removeTask(taskId);
                    });
                });
            }
            
            // 格式化时间间隔
            function formatElapsed(seconds) {
                const hours = Math.floor(seconds / 3600);
                const minutes = Math.floor((seconds % 3600) / 60);
                const secs = Math.floor(seconds % 60);
                
                let result = '';
                if (hours > 0) result += `${hours}h `;
                if (minutes > 0 || hours > 0) result += `${minutes}m `;
                result += `${secs}s`;
                
                return result;
            }
            
            // 开始克隆
            startCloneBtn.addEventListener('click', function() {
                const repoUrl = document.getElementById('github-repo-url').value;
                const branch = document.getElementById('github-branch').value || 'main';
                
                if (!repoUrl) {
                    showMessage('请输入GitHub仓库URL', 'warning');
                    return;
                }
                
                startGithubClone(repoUrl, branch);
            });
            
            // 清除已完成任务
            clearCompletedBtn.addEventListener('click', function() {
                clearTasks('completed');
            });
            
            // 清除失败任务
            clearFailedBtn.addEventListener('click', function() {
                clearTasks('failed');
            });
            
            // 开始GitHub克隆
            function startGithubClone(repoUrl, branch) {
                fetch('/api/github_clone', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        repo_url: repoUrl,
                        branch: branch
                    })
                })
                .then(res => {
                    if (!res.ok) {
                        throw new Error('克隆请求失败');
                    }
                    return res.json();
                })
                .then(data => {
                    if (data.status === 'success') {
                        showMessage('克隆任务已添加', 'success');
                        loadTasks();
                    } else {
                        showMessage(`添加任务失败: ${data.message}`, 'error');
                    }
                })
                .catch(error => {
                    console.error('添加任务失败:', error);
                    showMessage('添加任务失败: ' + error.message, 'error');
                });
            }
            
            // 取消任务
            function cancelTask(taskId) {
                showConfirm('确认取消', '确定要取消这个任务吗？', function() {
                    fetch('/api/github_clone/cancel_task', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({
                            task_id: taskId
                        })
                    })
                    .then(res => res.json())
                    .then(data => {
                        if (data.status === 'success') {
                            showMessage('任务已取消', 'success');
                            loadTasks();
                        } else {
                            showMessage(`取消任务失败: ${data.message}`, 'error');
                        }
                    })
                    .catch(error => {
                        console.error('取消任务失败:', error);
                        showMessage('取消任务失败', 'error');
                    });
                });
                
                return;
                
                fetch('/api/github_clone/cancel_task', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        task_id: taskId
                    })
                })
                .then(res => res.json())
                .then(data => {
                    if (data.status === 'success') {
                        showMessage('任务已取消', 'success');
                        loadTasks();
                    } else {
                        showMessage(`取消任务失败: ${data.message}`, 'error');
                    }
                })
                .catch(error => {
                    console.error('取消任务失败:', error);
                    showMessage('取消任务失败', 'error');
                });
            }
            
            // 移除任务
            function removeTask(taskId) {
                showConfirm('确认移除', '确定要从列表中移除这个任务吗？', function() {
                    fetch('/api/github_clone/clear_tasks', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({
                            type: 'single',
                            task_id: taskId
                        })
                    })
                    .then(res => res.json())
                    .then(data => {
                        if (data.status === 'success') {
                            showMessage('任务已移除', 'success');
                            loadTasks();
                        } else {
                            showMessage(`移除任务失败: ${data.message}`, 'error');
                        }
                    })
                    .catch(error => {
                        console.error('移除任务失败:', error);
                        showMessage('移除任务失败', 'error');
                    });
                });
                
                return;
                
                fetch('/api/github_clone/clear_tasks', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        type: 'single',
                        task_id: taskId
                    })
                })
                .then(res => res.json())
                .then(data => {
                    if (data.status === 'success') {
                        showMessage('任务已移除', 'success');
                        loadTasks();
                    } else {
                        showMessage(`移除任务失败: ${data.message}`, 'error');
                    }
                })
                .catch(error => {
                    console.error('移除任务失败:', error);
                    showMessage('移除任务失败', 'error');
                });
            }
            
            // 清除任务
            function clearTasks(type) {
                fetch('/api/github_clone/clear_tasks', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        type: type
                    })
                })
                .then(res => res.json())
                .then(data => {
                    if (data.status === 'success') {
                        showMessage(`已清除 ${data.removed_count} 个任务`, 'success');
                        loadTasks();
                    } else {
                        showMessage(`清除任务失败: ${data.message}`, 'error');
                    }
                })
                .catch(error => {
                    console.error('清除任务失败:', error);
                    showMessage('清除任务失败', 'error');
                });
            }
            
            // 初始加载任务
            loadTasks();
            
            // 每5秒刷新一次任务状态
            setInterval(loadTasks, 5000);
        });
    </script>

    <!-- 模态窗口 -->
    <div id="modal" class="modal fade" tabindex="-1" aria-labelledby="modal-label" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="modal-label">消息</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body" id="modal-body">
                    模态窗口内容
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 确认对话框 -->
    <div id="confirm-modal" class="modal fade" tabindex="-1" aria-labelledby="confirm-label" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="confirm-label">确认操作</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body" id="confirm-body">
                    您确定要执行此操作吗？
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="confirm-yes">确定</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 消息提示 -->
    <div id="message-box" class="position-fixed top-5 end-5 z-50 d-none">
        <div class="toast show" role="alert" aria-live="assertive" aria-atomic="true">
            <div class="toast-header">
                <strong class="me-auto" id="message-title">提示</strong>
                <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
            </div>
            <div class="toast-body" id="message-text">
                消息内容
            </div>
        </div>
    </div>

    <script>
        // 显示模态窗口
        function showModal(title, content) {
            const modal = document.getElementById('modal');
            document.getElementById('modal-label').textContent = title;
            document.getElementById('modal-body').textContent = content;
            new bootstrap.Modal(modal).show();
        }
        
        // 显示确认对话框
        function showConfirm(title, content, callback) {
            const confirmModal = document.getElementById('confirm-modal');
            document.getElementById('confirm-label').textContent = title;
            document.getElementById('confirm-body').textContent = content;
            
            // 移除之前的事件监听器
            const confirmYes = document.getElementById('confirm-yes');
            const newConfirmYes = confirmYes.cloneNode(true);
            confirmYes.parentNode.replaceChild(newConfirmYes, confirmYes);
            
            // 添加新的事件监听器
            newConfirmYes.addEventListener('click', function() {
                if (callback && typeof callback === 'function') {
                    callback();
                }
                bootstrap.Modal.getInstance(confirmModal).hide();
            });
            
            new bootstrap.Modal(confirmModal).show();
        }
        
        // 显示消息提示
        function showMessage(message, type = 'info') {
            const messageBox = document.getElementById('message-box');
            const messageTitle = document.getElementById('message-title');
            const messageText = document.getElementById('message-text');
            
            // 设置消息类型样式
            const toast = messageBox.querySelector('.toast');
            toast.className = 'toast show';
            
            // 设置标题和内容
            if (type === 'success') {
                messageTitle.textContent = '成功';
                messageTitle.className = 'me-auto text-success';
            } else if (type === 'error') {
                messageTitle.textContent = '错误';
                messageTitle.className = 'me-auto text-danger';
            } else if (type === 'warning') {
                messageTitle.textContent = '警告';
                messageTitle.className = 'me-auto text-warning';
            } else {
                messageTitle.textContent = '提示';
                messageTitle.className = 'me-auto';
            }
            
            messageText.textContent = message;
            messageBox.classList.remove('d-none');
            
            // 3秒后自动隐藏
            setTimeout(function() {
                messageBox.classList.add('d-none');
            }, 3000);
        }
        
        // 深色模式切换功能
        document.addEventListener('DOMContentLoaded', function() {
            const themeToggle = document.getElementById('theme-toggle');
            const themeIcon = themeToggle.querySelector('i');
            
            // 检查本地存储或系统偏好
            const savedTheme = localStorage.getItem('theme') || 
                            (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
            
            if (savedTheme === 'dark') {
                document.documentElement.setAttribute('data-theme', 'dark');
                themeIcon.className = 'bi bi-sun';
                themeToggle.innerHTML = '<i class="bi bi-sun"></i> 浅色模式';
            }
            
            // 切换主题
            themeToggle.addEventListener('click', function() {
                const currentTheme = document.documentElement.getAttribute('data-theme');
                if (currentTheme === 'dark') {
                    document.documentElement.removeAttribute('data-theme');
                    localStorage.setItem('theme', 'light');
                    themeIcon.className = 'bi bi-moon';
                    themeToggle.innerHTML = '<i class="bi bi-moon"></i> 深色模式';
                } else {
                    document.documentElement.setAttribute('data-theme', 'dark');
                    localStorage.setItem('theme', 'dark');
                    themeIcon.className = 'bi bi-sun';
                    themeToggle.innerHTML = '<i class="bi bi-sun"></i> 浅色模式';
                }
            });
        });
    </script>
</body>
</html>